import Ember from 'ember';
import Changeset from 'ember-changeset';
import EmployeeproformancesValidations from '../../../validations/employeeproformances';
import GoodsValidations from '../../../validations/goods';
import lookupValidator from 'ember-changeset-validations';

export default Ember.Controller.extend(EmployeeproformancesValidations,GoodsValidations, {
    constants: Constants,
    detailEdit: false,
    showEditBut: true,
    delFlag: true,
    performanceItemList: [],
    performanceDetialList: [],
    dateService: Ember.inject.service("date-service"),
    dataLoader:Ember.inject.service('data-loader'),
    store: Ember.inject.service("store"),
    employeeproformancesModel:Ember.computed("employeeproformancessInfo",function(){
      var model = this.get("employeeproformancessInfo");
      if (!model) {
        return null;
      }
      return new Changeset(model, lookupValidator(EmployeeproformancesValidations), EmployeeproformancesValidations);
    }),
    employeeproformancesModelObs:function(){
      var model = this.get("employeeproformancessInfo");
      if (!model) {
        return null;
      }
      let employeeproformancesModel = new Changeset(model, lookupValidator(EmployeeproformancesValidations), EmployeeproformancesValidations);
      this.set('employeeproformancesModel',employeeproformancesModel);
    },
    actions: {
        invalid() {
            //alert("invalid");
        },
        invitation(){
          this.set('addNewGoodsModel',false);
        },
        //编辑
        detailEditClick(){
          this.set('detailEdit',true);
          this.set("showFormanceType", false);
          this.set("showPerformanceItem", false);
          this.set("showPerformanceDetial", false);
          this.employeeproformancesModelObs();
          this.set('performanceType', this.get('employeeproformancesModel.type'));
          this.set('curPerformanceItem', this.get('employeeproformancesModel.achievementsTypeItem.achievementsType'));
          this.set('curPerformanceDetial',this.get('employeeproformancesModel.achievementsTypeItem'));
          this.send('employeeproformancesTypeSelect', this.get('employeeproformancesModel.type'), true);
        },
        //删除
        delById(){
          var _self = this;
          App.lookup('controller:business.mainpage').showConfirm("是否确定删除此记录",function(){
            _self.send('cancelPassSubmit',_self.get('employeeproformancesModel'));
          });
        },
        cancelPassSubmit(employeeproformances){
          App.lookup('controller:business.mainpage').openPopTip("正在删除");
          let employeeproformancesModel = this.get('employeeproformancesModel');
          employeeproformancesModel.set("delStatus", 1);
          employeeproformancesModel.save().then(function() {
            App.lookup('controller:business.mainpage').showPopTip("删除成功");
            var mainpageController = App.lookup('controller:business.mainpage');
            mainpageController.switchMainPage('performance-base');
          },function(err){
            let error = err.errors[0];
            if(error.code === '14'){
              App.lookup('controller:business.mainpage').showAlert('此绩效记录已经统计，不能删除');
            }
          });
        },
        //选择员工
        selectEmployee(employee){
          this.set('employeeproformancesModel.employee',employee);
          this.set("salaryPositionName",employee.get("salaryPosition.name"));
          this.set("salaryPositionObj",employee.get("salaryPosition"));
        },
        //选择绩效类型
        employeeproformancesTypeSelect(type, flag){
          const _self = this;
          this.set('performanceType',type);
          this.set('showFormanceType',false);
          this.set('showPerformanceItem',false);
          this.set('showPerformanceDetial',false);
          this.set('performanceItemList', []);
          this.set('employeeproformancesModel.type',type);
          this.store.query('employeeachievementstype', {
            filter: {'[type][typecode]': type.get('typecode')}
          }).then(function(performanceItemList){
            console.log('performanceItemList11:', performanceItemList);
            _self.set("performanceItemList", performanceItemList);
            _self.set('showFormanceType',true);
            // if (flag) {
            //   _self.send('selectPerformanceItem', _self.get('employeeproformancesModel.achievementsTypeItem.achievementsType'), true);
            // }
          });
        },
        selectPerformanceItem(item, flag){
          const _self = this;
          this.set('curPerformanceItem',item);
          this.set('showPerformanceItem',false);
          this.set('showPerformanceDetial',false);
          this.set('performanceDetialList', []);
          this.get('employeeproformancesModel').set('typeName',item.get('name'));
          // this.set('employeeproformancesModel.achievementsType',item);
          this.store.query('employeeachievementstypeitem', {
            filter: {'[achievementsType][id]': item.get('id')}
          }).then(function(performanceDetialList){
            console.log('performanceDetialList11:', performanceDetialList);
            _self.set("performanceDetialList", performanceDetialList);
            _self.set('showPerformanceItem',true);
            // if (flag) {
            //   console.log('2222:', );
            //   _self.send('selectPerformanceDetial', _self.get('employeeproformancesModel.achievementsTypeItem'));
            // }
          });
        },
        selectPerformanceDetial(item){
          this.set('curPerformanceDetial',item);
          this.set('showPerformanceDetial',true);
          console.log('item10001::', item);
          console.log('item111::', item, item.get('remark'));
          // this.set('employeeproformancesModel.achievementsTypeItem',item);
          this.get('employeeproformancesModel').set('typeItemName',item.get('remark'));
          this.set('employeeproformancesModel.amount',item.get('money'));
        },
        //保存采购信息
        saveEmployeeproformances(){
          let _self = this;
          let employeeproformancesModel = this.get('employeeproformancesModel');
          let salaryPositionObj = this.get("salaryPositionObj");
          employeeproformancesModel.set("salaryPosition",salaryPositionObj);
          employeeproformancesModel.set("createUser",this.get('createUser'));
          let editMode = this.get('editMode');
          employeeproformancesModel.validate().then(function(){
            if(employeeproformancesModel.get('errors.length')===0){
              App.lookup('controller:business.mainpage').openPopTip("正在保存");
              employeeproformancesModel.save().then(function(){
                App.lookup('controller:business.mainpage').showPopTip("保存成功");
                if(editMode == 'edit'){
                  _self.set('detailEdit',false);
                  var route=App.lookup('route:business.mainpage.performance-base-detail');
                  route.refresh();//刷新页面
                }else if(editMode == 'add'){
                  var mainpageController = App.lookup('controller:business.mainpage');
                  mainpageController.switchMainPage('performance-base');
                }
              },function(err){
                App.lookup('controller:business.mainpage').showPopTip("保存失败");
              });
            }else{
              employeeproformancesModel.set("validFlag",Math.random());
            }
          });
        },
        //取消
        detailCancel(){
          let editMode = this.get('editMode');
          if(editMode == 'edit'){
            this.get('employeeproformancessInfo').rollbackAttributes();
            this.set('detailEdit',false);
            var route=App.lookup('route:business.mainpage.performance-base-detail');
            route.refresh();//刷新页面
          }else if(editMode == 'add'){
            var mainpageController = App.lookup('controller:business.mainpage');
            mainpageController.switchMainPage('performance-base');
          }
        },
    }
});
